<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="800px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
        <el-tabs v-model="activeName" class="demo-tabs">
          <el-tab-pane label="基本">
            <el-form ref="formRef" :model="formData" label-width="120px">
                <el-form-item label="父级菜单" prop="pid">
                    <el-tree-select
                        class="flex-1"
                        v-model="formData.pid"
                        :data="menuOptions"
                        clearable
                        node-key="id"
                        :props="{
                            label: 'name'
                        }"
                        :default-expand-all="true"
                        placeholder="请选择父级菜单"
                        check-strictly
                    />
                </el-form-item>
                <el-form-item label="栏目名称" prop="name">
                    <el-input
                        v-model="formData.name"
                        placeholder="请输入栏目名称"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="SEO标题" prop="seo_name">
                    <el-input
                        v-model="formData.seo_name"
                        placeholder="请输入SEO标题"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="SEO关键字" prop="seo_kword">
                    <el-input
                        v-model="formData.seo_kword"
                        placeholder="请输入SEO关键字"
                        clearable
                    />
                </el-form-item>
               
                <el-form-item label="缩略图" prop="img">
                    <material-picker
                        v-model="formData.img"
                        :limit="1"
                    />
                </el-form-item>
                <el-form-item label="简介" prop="brief">
                    <el-input
                        v-model="formData.brief"
                        placeholder="请输入简介"
                        :rows="2"
                        type="textarea"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="内容" prop="text">
                    <editor style="height:400px;" v-model="formData.text"/>
                </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="拓展">
            <el-form ref="formRef" :model="formData" label-width="120px">
                <el-form-item label="排序" prop="sort">
                        <el-input
                            v-model="formData.sort"
                            placeholder="请输入排序"
                            clearable
                        />
                </el-form-item>
                <el-form-item label="栏目模版" prop="clumn_plate">
                    <el-tree-select
                        class="flex-1"
                        v-model="formData.clumn_plate"
                        :data="menuOption"
                        clearable
                        node-key="id"
                        :props="{
                            value: 'plate',
                            label: 'plate',
                        }"
                        check-strictly
                        :default-expand-all="true"
                        placeholder="请选择栏目模版"
                    />
                </el-form-item>
                <el-form-item label="详情页模版" prop="details_plate">
                    <el-tree-select
                        class="flex-1"
                        v-model="formData.details_plate"
                        :data="menuOption"
                        clearable
                        node-key="id"
                        :props="{
                            value: 'plate',
                            label: 'plate',
                        }"
                        check-strictly
                        :default-expand-all="true"
                        placeholder="请选择详情页模版"
                    />
                </el-form-item>
                <el-form-item label="栏目外链" prop="link">
                        <el-input
                            v-model="formData.link"
                            placeholder="请输入栏目外链"
                            clearable
                        />
                </el-form-item>
                <el-form-item  label="显示" prop="is_disable">
                    <div class="w-80">
                                <el-radio-group
                                    v-model="
                                        formData.is_open
                                    "
                                >
                                    <el-radio :label="1">显示</el-radio>
                                    <el-radio :label="2">隐藏</el-radio>
                                </el-radio-group>
                            </div>
                </el-form-item >
            </el-form>
          </el-tab-pane>
        </el-tabs>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import Popup from '@/components/popup/index.vue'
import type { FormInstance } from 'element-plus'
import { detailClumn, setClumn,clumnPlate,wholeClume,getClumn } from '@/api/application/website/column'
import { arrayToTree, treeToArray } from '@/utils/util'
import { MenuEnum } from '@/enums/appEnums'
const activeName = ref('0')
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑栏目' : '新增栏目'
})
const formData = reactive({
    id:0,
    seo_name:"",
    seo_kword:"",
    img:"",
    brief:"",
    text:"",
    clumn_plate:"",
    details_plate:"",
    is_display:1,
    is_open:1,
    sort:"",
    link:"",
    pid:"",
    name:""
})
const menuOption = ref<any[]>([])
const getLists = async()=>{
        const data: any = await clumnPlate({})
        menuOption.value = data
}
getLists()
const menuOptions = ref<any[]>([])
const getList = async()=>{
    const data: any = await wholeClume({})
    const menu: any = { id: 0, name: '顶级栏目', children: [] }
    menu.children = arrayToTree(
        treeToArray(data).filter((item) => item.type != MenuEnum.BUTTON)
    )
    menuOptions.value.push(menu)
}
getList()
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const handleSubmit = async () => {
    await formRef.value?.validate()
    await setClumn(formData)
    popupRef.value?.close()
    emit('success')
}
const getDetail = async (row: Record<string, any>) => {
    const data = await detailClumn({
        id: row.id
    })
    setFormData(data)
}
const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}
const handleClose = () => {
    emit('close')
}
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
